package com.example.dollarShipping.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dollarShipping.entity.OperationRecord;
import com.example.dollarShipping.entity.ShippingOrder;
import com.example.dollarShipping.entity.TruckOrder;
import com.example.dollarShipping.entity.User;
import com.example.dollarShipping.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AccountService {
    @Resource
    ShippingOrderMapper shippingOrderMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    OperationRecordMapper operationRecordMapper;
    @Resource
    TruckOrderMapper truckOrderMapper;

    public List<ShippingOrder> getShippingOrder(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        if(id!=null) wrapper.eq("creator_id", id);
        List<ShippingOrder> shippingOrder = shippingOrderMapper.selectList(wrapper);
        shippingOrder.stream().map(s -> {
            List<OperationRecord> sl = operationRecordMapper.selectList(new QueryWrapper<OperationRecord>().eq("type",1).eq("order_id",s.getId()));
            List<TruckOrder> truckOrder = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
            for(TruckOrder t:truckOrder){
                List<OperationRecord> tl = operationRecordMapper.selectList(new QueryWrapper<OperationRecord>().eq("type",2).eq("order_id",t.getId()));
                t.setOperationRecords(tl);
            }
            s.setOperationRecords(sl);
            s.setTruckOrder(truckOrder);
            return s;
        }).collect(Collectors.toList());
        return shippingOrder;
    }

    public void createShippingOrder(Long creatorId, String start, String end, String consignee, Float distance, String goods, Float weight, String desc) {
        User u = userMapper.selectById(creatorId);
        ShippingOrder s = new ShippingOrder(creatorId,u.getRealName(),start,end,consignee,distance,desc,goods,weight,new Date());
        shippingOrderMapper.insert(s);
    }


    public String changeState(Integer state, Long id, Long operator) {
        ShippingOrder s = shippingOrderMapper.selectById(id);
        String res = null;
        switch (state) {
            case 8:
                s.setState(7);
                s = settle(s);
                res = "已签收";
                break;
            case 9:
                s.setState(8);
                res = "结算完成";
        }
        shippingOrderMapper.updateById(s);
        OperationRecord task = new OperationRecord(1,id,state,operator,new Date());
        operationRecordMapper.insert(task);
        return res;
    }

    private ShippingOrder settle(ShippingOrder s) {
        List<TruckOrder> tl = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
        double unexpectedCost = 0;
        for(TruckOrder t:tl){
            unexpectedCost+=t.getUnexpectedCost();
        }
        s.setUnexpectedCost(unexpectedCost);
        s.setTotalPrice(s.getLogisticsCost()+unexpectedCost);
        return s;
    }


}
